 /*
  * Team Java
  * This is the IO that generats interupts for the scheduler to handle. 
  * Author(s): Michael Satran Jr, Michael Pogson, Stephen Hunter
  * Date: 5/31/2012
  */

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h> 
#include "io.h"
#include <string.h>

void *diskInterupt(void *runningtime){

	int i = 0;
	while( i < 1000) {
	
		i++;
	}
	printf("disk needs some service! \n");
	interuptHandler(1);
} 

void *videoInterupt(void *runningtime){
		
	int i = 0;
	while( i < 100) {
	
		i++;
	}
	
	printf("video needs some service! \n");
	
	interuptHandler(2);
}


void *keyInterupt(){
	
    int keypressed;
    int thekey = 'n';
	int answer = 0;
	while(answer != 1) {
	
		keypressed = getchar();
		if(keypressed == thekey){
			printf("Your key and the interupt key were the same!\n");
			answer = 1;
		}
	
	}
	
	printf("Key needs some service\n");
	interuptHandler(3);
}

void *handleInterupt(){

	printf("Handling the interupt\n");
	
	int i;
	while(i<10){
		i++;
	
	}
	
	printf("Interupt handled returning back to scheduler\n");
	
}

void *start() {
	
	pthread_attr_t attr;
	void *status;
	
	pthread_t threads[5];
	pthread_t rthreads[5];
	
	pthread_attr_init(&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
	
	int rc;
	int i;
	for(i = 0; i < 1; i++){
		printf("\n");
		pthread_create(&threads[i], &attr, keyInterupt, NULL);
		pthread_create(&rthreads[i], &attr, videoInterupt, (void *)100);
		pthread_create(&rthreads[i +1], &attr, diskInterupt, (void *)100);		
	}
	
	for(i = 0; i < 1; i++) {
	printf("\n");
	pthread_join(threads[i], &status);
	pthread_join(rthreads[i],&status);
	}
	
	pthread_attr_destroy(&attr);
    pthread_exit(NULL);
}

void *systemCall() {
	
	
	int i;
	while(i<150){
		i++;
	
	}
	
	printf("Process needs its IO call handled!\n");
	




}
